package com.zzx.threekingdom.model;

import com.zzx.threekingdom.card.Card;
import com.zzx.threekingdom.judge.Rule.CardRule;

import java.util.ArrayList;

public class ActionInfo
{
    public AbsPlayerModel from;
    public AbsPlayerModel to;
    
    public ArrayList<AbsPlayerModel> additionalToPlayer = new ArrayList<AbsPlayerModel>();
    public String actionName;
    public Card card;
    
    public ActionInfo()
    {
        clear();
    }
    public ActionInfo(AbsPlayerModel f, AbsPlayerModel t, String a, Card c)
    {
        from = f;
        to = t;
        actionName = a;
        card = c;
    }
    public void clear()
    {
        from = null;
        to = null;
        actionName = null;
        card = null;
        additionalToPlayer.clear();
    }
    
    public AbsPlayerModel getFrom() { return from; };
    public AbsPlayerModel getTo() { return to; };
    public String getActionName() { return actionName; };
    public Card getCard() { return card; };
    public ArrayList<AbsPlayerModel> getAdditionalToPlayer() { return additionalToPlayer; }
    public ActionInfo setFrom(AbsPlayerModel pm) { from = pm; return this; };
    public ActionInfo setTo(AbsPlayerModel pm) { to = pm; return this; };
    public ActionInfo setActionName(String n) { actionName = n; return this; };
    public ActionInfo setCard(Card c) { card = c; return this; };
    public ActionInfo addAdditionalToPlayer(AbsPlayerModel pm) { additionalToPlayer.add(pm); return this; }
    public boolean isTargetValid() { return (getTargetPms().size() >= CardRule.geTargetNumByActionName(actionName)); }
    
    public String getHintText()
    {
        if(actionName != null)
        {
            if(from!=null)
                return (from.getCharacterName() + ((to==null)?"":("对" + to.getCharacterName())) + "使用" + actionName);
            else if(to != null)
                return to.getCharacterName() + "被 " + actionName;
            else
                return actionName;
        }

        return "";
    }
    
    public ArrayList<AbsPlayerModel> getTargetPms()
    {
        ArrayList<AbsPlayerModel> ret = new ArrayList<AbsPlayerModel>();
        if(to!=null)
            ret.add(to);
        for(int i = 0;i < additionalToPlayer.size();i++)
            ret.add(additionalToPlayer.get(i));
        return ret;
    }
    
    public AbsPlayerModel getOneTarget()
    {
        if(to!=null)
            return to;
        else if(additionalToPlayer.size()!=0)
        	return additionalToPlayer.get(0);
        else
        	return null;
    }
    
    public void addTargetPms(ArrayList<AbsPlayerModel> pms)
    {
        if(pms==null)
            return;
        for(int i = 0;i < pms.size();i++)
        {
            AbsPlayerModel pm = pms.get(i); 
            if(pm != null)
            {
                if(to==null)
                    to = pm;
                else
                    addAdditionalToPlayer(pm);
            }
        }
    }
    
    
}
